package com.example.demo.mathtest;

import java.util.ArrayList;

public class BinaryTree {
    private Node root;
    private int num;
    private ArrayList<Node> opList = new ArrayList<Node>();

    public BinaryTree(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public void setTreeNode(Node root) {
        this.root = root;
    }

    public String toString() {
        String str = root.toString();
        str = str.substring(1, str.length() - 1);
        return str;
    }

    public String CalAndVal() {
        return root.getResult();
    }

    public int getDeep() {
        int i = this.num;
        int deep = 2;
        while (i / 2 > 0) {
            deep++;
            i /= 2;
        }
        return deep;
    }

    public void createBTree() {
        Node lChild, rChild, lNode, rNode;

        if (num == 1) {
            lChild = new Node(String.valueOf(Ran.getNumber(10)), null, null);
            rChild = new Node(String.valueOf(Ran.getNumber(10)), null, null);
            root = new Node(String.valueOf(Ran.getOperator()), lChild, rChild);
        } else {
            int num1 = 0;
            int n = getDeep() - 3;
            boolean[] place = Ran.getChildPlace(num);
            root = new Node(String.valueOf(Ran.getOperator()), null, null);
            opList.add(root);

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < (int) Math.pow(2, i); j++, num1++) {
                    lChild = new Node(String.valueOf(Ran.getOperator()), null, null);
                    rChild = new Node(String.valueOf(Ran.getOperator()), null, null);
                    opList.get(j + num1).setChild(lChild, rChild);
                    opList.add(lChild);
                    opList.add(rChild);
                }
            }

            for (int i = 0; i < place.length; i++) {
                if (place[i]) {
                    lNode = new Node(String.valueOf(Ran.getNumber(10)), null, null);
                    rNode = new Node(String.valueOf(Ran.getNumber(10)), null, null);
                    if (i % 2 == 0) {
                        lChild = new Node(String.valueOf(Ran.getOperator()), lNode, rNode);
                        opList.add(lChild);
                        opList.get(num1).setlChild(lChild);
                    } else {
                        rChild = new Node(String.valueOf(Ran.getOperator()), lNode, rNode);
                        opList.add(rChild);
                        opList.get(num1).setRChild(rChild);
                    }
                } else {
                    if (i % 2 == 0) {
                        lChild = new Node(String.valueOf(Ran.getNumber(10)), null, null);
                        opList.get(num1).setlChild(lChild);
                    } else {

                        rChild = new Node(String.valueOf(Ran.getNumber(10)), null, null);
                        opList.get(num1).setRChild(rChild);
                    }
                }
                num1 = num1 + i % 2;
            }
        }
    }
}